var cache = {};
$(function() {
	$('.doubledate').kuiDate({
		className : 'doubledate',
		isDisabled : "0" // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选
	});

	qmsk.initSelect({
		url : '../memberState/find/all/0/100',
		id : 'searchMemberState',
		heads : ['id', 'name'],
		defaults : {
			key : '所有',
			value : ' '
		}
	});
	var shopId = $("#shopName").val();
	if (IsNotEmpty(shopId)) {
		qmsk.initSelect({
			url : '../memberLevel/find/memberLevelList/0/10?shopId=' + shopId,
			id : 'searchMemberLevel',
			heads : ['ID', 'Name'],
			defaults : {
				key : '请选择会员级别',
				value : ' '
			}
		});
	}
	initShopSelecter();
});
/**
 * 初始化店面选择器
 */
function initShopSelecter() {
	refreshInput($(".serWrapper"));
	$("#shopIsChoosed").removeAttr("checked");
	$("#shopName").attr("disabled", "disabled");

	$("#shopIsChoosed").bind("click", function() {
		var checked = $(this).attr("checked");
		if (checked) {
			$("#shopName").removeAttr("disabled");
		} else {
			$("#shopName").attr("disabled", "disabled");
			$("#shopName").empty();
			$("#searchLeave").empty();
		}
	});
}

/**
 * 店面选择后的回调函数，这里需要去加载会员级别下拉框
 */
function shopSelected(shopId, shopName) {
	qmsk.initSelect({
		url : '../memberLevel/find/memberLevelList/0/10?shopId=' + shopId,
		id : 'searchMemberLevel',
		heads : ['ID', 'Name'],
		defaults : {
			key : '请选择会员级别',
			value : ' '
		}
	});
}

/**
 * 分页查询
 */
function search() {

	var shopId = $("#shopName").val();
	if (IsEmpty(shopId)) {
		shopId = '';
	}
	var customerName = $("#searchName").val();
	var mp = $("#searchMp").val();
	var memberCardNo = $("#searchMemberCardNo").val();
	var levelId = $("#searchMemberLevel").val();
	if (IsEmpty(levelId)) {
		levelId = '';
	}
	var status = $("#searchMemberState").val();
	if (IsEmpty(status)) {
		status = '';
	}

	var isWechat = $("#isWechat").val();
	if (IsEmpty(isWechat)) {
		isWechat = '';
	}

	var license = $("#searchLicense").val();

	var IntegralAmount1 = $("#searchIntegral1").val();
	var IntegralAmount2 = $("#searchIntegral2").val();
	var cashCouponAmount1 = $("#searchCashCoupon1").val();
	var cashCouponAmount2 = $("#searchCashCoupon2").val();
	var depositAmount1 = $("#searchDeposit1").val();
	var depositAmount2 = $("#searchDeposit2").val();
	var createFileTime1 = $("#createFileTime1").val();
	var createFileTime2 = $("#createFileTime2").val();
	qmsk.get('../memberQuery/find/amount', {
		shopId : shopId,
		customerName : customerName,
		mp : mp,
		memberCardNo : memberCardNo,
		levelId : levelId,
		status : status,
		license : license,
		IntegralAmount1 : IntegralAmount1,
		IntegralAmount2 : IntegralAmount2,
		cashCouponAmount1 : cashCouponAmount1,
		cashCouponAmount2 : cashCouponAmount2,
		depositAmount1 : depositAmount1,
		depositAmount2 : depositAmount2,
		createFileTime1 : createFileTime1,
		createFileTime2 : createFileTime2,
		isWechat : isWechat
	}, function(response) {

		//"depositAmountSum","cashCouponAmountSum",
		//"CouponAmountSum","consumeAmountSum","integralAmountSum"
		var depositAmountSum = response.getValue("depositAmountSum");
		if (IsEmpty(depositAmountSum)) {
			$("#sum").hide();
			$("#noSum").show();
		} else {
			$("#sum").show();
			$("#noSum").hide();
		}

		var cashCouponAmountSum = response.getValue("cashCouponAmountSum");
		var CouponAmountSum = response.getValue("CouponAmountSum");
		var consumeAmountSum = response.getValue("consumeAmountSum");
		var integralAmountSum = response.getValue("integralAmountSum");
		var text = '储值总计：' + depositAmountSum;
		text += '    积分总计：' + integralAmountSum;
		text += '    代金券总计：' + cashCouponAmountSum;
		text += '    优惠券总计：' + CouponAmountSum;
		$("#sum").text(text);
	});

	new Pagination({
		url : '../memberQuery/find/list1',
		data : {
			shopId : shopId,
			customerName : customerName,
			mp : mp,
			memberCardNo : memberCardNo,
			levelId : levelId,
			status : status,
			license : license,
			IntegralAmount1 : IntegralAmount1,
			IntegralAmount2 : IntegralAmount2,
			cashCouponAmount1 : cashCouponAmount1,
			cashCouponAmount2 : cashCouponAmount2,
			depositAmount1 : depositAmount1,
			depositAmount2 : depositAmount2,
			createFileTime1 : createFileTime1,
			createFileTime2 : createFileTime2,
			isWechat : isWechat
		},
		success : printDataToPage,
		pagerHandler : 'pager'
	});
}

function printDataToPage(rst) {
	//"shopID","shopName","customerID","customerName","sex","mp","tel","birthday","rmail",
	//"zipCode","IDNumber","industryType","companyName","companyDuty","address","custFrom","levelID",
	//"levelName","memberFee","status","memberStateName","createFileTime","validDateTime",
	//"validityDay","memberCardNo","payPwd","password","memberMemo",
	//"depositAmount","cashCouponAmount","couponAmount","consumeAmount",
	//"integralAmount","discount","carTypeName","license"
	var response = new qmsk.Response(rst);
	qmsk.dataTable({
		tbodyId : 'memberList',
		heads : ["createFileTime", "memberCardNo", "customerName", "mp", "IDNumber", "levelName", "integralAmount", "depositAmount", "cashCouponAmount", "couponCount", "discount", "memberStateName", ["shopID", "memberID", "customerID"]],
		response : response,
		sup : ['sup', '共 [index] 条'],
		handler : [[12, detailButton]]
	});
}

function detailButton(shopID, memberID, customerID, row) {
	return "<input type='button' value='详情' onclick='toDetail(" + shopID + "," + memberID + "," + customerID + ");'  class='common_btn'>";
}

function toDetail(shopId, memberID, customerID) {
	cache.memberId = memberID;
	cache.shopId = shopId;
	qmsk.get('../memberQuery/find/detail/' + memberID + '/' + shopId, printDetailToPage);
	qmsk.get('../qrcode/find/sceneId/' + customerID, function(response) {
		//"sceneId","title"
		var title = response.getValue("title");
		if (qmsk.isEmpty(title)) {
			title = "其他";
		}
		$("#addCustomerFrom").val(title);

	});
	openDetailPanel();
}

/**
 * 将详细信息显示到页面上
 */
function printDetailToPage(response) {
	var rst = response.rst;
	$("#addShopId").val(getHtmlString(getHeadIndexValue(0, rst, "shopName")));
	$("#addCustomerName").val(getHtmlString(getHeadIndexValue(0, rst, "customerName")));
	$("#addSex").val(getHtmlString(getHeadIndexValue(0, rst, "sex")));
	$("#addMp").val(getHtmlString(getHeadIndexValue(0, rst, "mp")));
	$("#addTel").val(getHtmlString(getHeadIndexValue(0, rst, "tel")));

	$("#addBirthday").val(getHtmlString(getHeadIndexValue(0, rst, "birthday")));
	$("#addEmail").val(getHtmlString(getHeadIndexValue(0, rst, "email")));
	$("#addZipCode").val(getHtmlString(getHeadIndexValue(0, rst, "zipCode")));

	$("#addIDNumber").val(getHtmlString(getHeadIndexValue(0, rst, "IDNumber")));
	$("#addIndustryType").val(getHtmlString(getHeadIndexValue(0, rst, "industryType")));
	$("#addCompanyName").val(getHtmlString(getHeadIndexValue(0, rst, "companyName")));

	$("#addCompanyDuty").val(getHtmlString(getHeadIndexValue(0, rst, "companyDuty")));
	$("#addAddress").val(getHtmlString(getHeadIndexValue(0, rst, "address")));
	//$("#addCustomerFrom").val(getHtmlString(getHeadIndexValue(0, rst, "custFrom")));

	$("#addLevelId").val(getHtmlString(getHeadIndexValue(0, rst, "levelName")));
	$("#memberFee").val(getHtmlString(getHeadIndexValue(0, rst, "memberFee")));
	$("#addMemberStatus").val(getHtmlString(getHeadIndexValue(0, rst, "memberStateName")));

	$("#createFileTime").val(getHtmlString(getHeadIndexValue(0, rst, "createFileTime")));
	$("#validDay").val(getHtmlString(getHeadIndexValue(0, rst, "validityDay")));
	$("#validDateTime").val(getHtmlString(getHeadIndexValue(0, rst, "validDateTime")));

	$("#addMemberCardNO").val(getHtmlString(getHeadIndexValue(0, rst, "memberCardNo")));

	$("#addNote").val(getHtmlString(getHeadIndexValue(0, rst, "memberMemo")));
	$("#depositAmount").val(getHtmlString(getHeadIndexValue(0, rst, "depositAmount")));
	$("#couponAmount").val(getHtmlString(getHeadIndexValue(0, rst, "cashCouponAmount")));

	$("#consumeAmount").val(getHtmlString(getHeadIndexValue(0, rst, "consumeAmount")));
	$("#integralAmount").val(getHtmlString(getHeadIndexValue(0, rst, "integralAmount")));
	$("#discount").val(getHtmlString(getHeadIndexValue(0, rst, "discount")));
	$("#saleAdvisor").val(getHtmlString(getHeadIndexValue(0, rst, "saleAdvisor")));

	//"advisorId","saleAdvisorId","saleAdvisor"
	cache.advisorId = response.getValue('advisorId');
	cache.saleAdvisorId = response.getValue('saleAdvisorId');

}

//------------------------会员详情----------------------------------------
/**
 * 显示详情面板
 */
function openDetailPanel() {
	$("#DetailPanel").show();
	$("#listPanel").hide();
}

/**
 * 关闭详情面板
 */
function closeDetailPanel() {
	$("#DetailPanel").hide();
	$("#listPanel").show();
}

/**
 * 加载车辆信息列表
 */
function loadCarList(customerId) {
	if (customerId == null) {
		return;
	}
	qmsk.get('../custCar/find/list/customerId/' + customerId, function(rst) {
		dataTable({
			tbodyId : 'customerCarTbody',
			heads : ["carTypeName", "license", "insuranceFirstDate", "insuranceEndDate", "recentlyDate", "checkCarDate"],
			rst : rst
		});
	});
}

function down() {
	var shopId = $("#shopName").val();
	if (qmsk.isEmpty(shopId)) {
		artAlert('请选择店面');
		return;
	}
	var customerName = $("#searchName").val();
	var mp = $("#searchMp").val();
	var memberCardNo = $("#searchMemberCardNo").val();
	var levelId = $("#searchMemberLevel").val();
	if (IsEmpty(levelId)) {
		levelId = '';
	}
	var status = $("#searchMemberState").val();
	if (IsEmpty(status)) {
		status = '';
	}

	var isWechat = $("#isWechat").val();
	if (IsEmpty(isWechat)) {
		isWechat = '';
	}

	//var license = $("#searchLicense").val();

	var IntegralAmount1 = $("#searchIntegral1").val();
	var IntegralAmount2 = $("#searchIntegral2").val();
	var cashCouponAmount1 = $("#searchCashCoupon1").val();
	var cashCouponAmount2 = $("#searchCashCoupon2").val();
	var depositAmount1 = $("#searchDeposit1").val();
	var depositAmount2 = $("#searchDeposit2").val();
	var createFileTime1 = $("#createFileTime1").val();
	var createFileTime2 = $("#createFileTime2").val();

	var data = {
		shopId : shopId,
		customerName : customerName,
		mp : mp,
		memberCardNo : memberCardNo,
		levelId : levelId,
		status : status,
		//license : license,
		IntegralAmount1 : IntegralAmount1,
		IntegralAmount2 : IntegralAmount2,
		cashCouponAmount1 : cashCouponAmount1,
		cashCouponAmount2 : cashCouponAmount2,
		depositAmount1 : depositAmount1,
		depositAmount2 : depositAmount2,
		createFileTime1 : createFileTime1,
		createFileTime2 : createFileTime2,
		isWechat : isWechat
	};
	qmsk.redirectUrl('../memberQuery/down', data);
}

function userSelecter(self, dutyName) {

	var map = {
		salesConsultant : '置业顾问',
		serviceConsultant : '服务顾问',
		customerManager : '客户经理',
		customerServiceManager : '客服经理',
		recommendMan : ''
	};

	$(self).blur();
	initSelecter({
		method : 'post',
		url : '../user/find/userSelecter',
		data : {
			dutyName : map[dutyName]
		},
		heads : ["customerName", "loginName"],
		titles : ["用户名称", "登录名称"],
		zindex : 500,
		callBack : function(response, row) {
			var id = response.getValue('customerId', row);
			var name = response.getValue('customerName', row);
			$(self).val(name);
			cache.saleAdvisorId = id;
		}
	});
}

function editSaleAdvisor() {

	var id = cache.advisorId;
	var userId = cache.saleAdvisorId;

	if (qmsk.isEmpty(id)) {
		var memberId = cache.memberId;
		var shopId = cache.shopId;
		qmsk.post('../myAdvisor/insert/' + shopId + '/' + memberId + '/' + userId, function(response) {
			cache.advisorId = response.getValue("id");
			alert(cache.saleAdvisorId);
			artSuccess("添加成功！");
		});
	} else {
		qmsk.post('../myAdvisor/update/' + id + '/' + userId, function() {
			artSuccess("更新成功！");
		});
	}

}
